home *** CD-ROM | disk | FTP | other *** search
/ Champak 43 / Vol 43.iso / games / phit.swf / scripts / __Packages / CSimulation.as < prev    next >
Encoding:
Text File  |  2007-07-13  |  7.0 KB  |  268 lines

  1. class CSimulation
  2. {
  3.    var m_arrClocks;
  4.    var m_arrDeltaTimes;
  5.    var m_arrTimeScalars;
  6.    var m_arrIsPaused;
  7.    var m_vecGravity;
  8.    var m_bodies;
  9.    var m_simulatedThings;
  10.    var m_collisionResolver;
  11.    var m_nUpdates = 0;
  12.    var m_timeSecondsReal = 0;
  13.    var m_timeSecondsSimulated = 0;
  14.    var m_frameDeltaTimeReal = 0;
  15.    var m_frameDeltaTimeSimulated = 0;
  16.    var m_constantFrameDeltaTime = 0;
  17.    var m_timeScalar = 1;
  18.    var m_isPaused = false;
  19.    var m_bCheckCollisions = true;
  20.    var m_shouldSortThings = false;
  21.    var m_maxFrameDeltaTime = 0.1;
  22.    function CSimulation(collisionResolver)
  23.    {
  24.       this.m_timeSecondsReal = new Date().getTime() / 1000;
  25.       this.m_timeScalar = 1;
  26.       this.m_arrClocks = new Array();
  27.       this.m_arrDeltaTimes = new Array();
  28.       this.m_arrTimeScalars = new Array();
  29.       this.m_arrIsPaused = new Array();
  30.       this.m_vecGravity = new Vector2D(0,0);
  31.       this.m_bodies = new Array();
  32.       this.m_simulatedThings = new Array();
  33.       if(collisionResolver)
  34.       {
  35.          this.m_collisionResolver = collisionResolver;
  36.       }
  37.       else
  38.       {
  39.          this.m_collisionResolver = new CCollisionResolverBucketSort(8,6,800,600,0,0);
  40.       }
  41.    }
  42.    function DestroyAllBodies()
  43.    {
  44.       while(this.m_bodies.length > 0)
  45.       {
  46.          this.m_bodies[0].Destroy();
  47.       }
  48.    }
  49.    function CreateNamedClock(name)
  50.    {
  51.       this.m_arrClocks[name] = 0;
  52.       this.m_arrDeltaTimes[name] = 0;
  53.       this.m_arrTimeScalars[name] = 1;
  54.       this.m_arrIsPaused[name] = false;
  55.    }
  56.    function HasNamedClock(name)
  57.    {
  58.       return this.m_arrClocks[name] != undefined;
  59.    }
  60.    function SetPausedNamedClock(name, isPaused)
  61.    {
  62.       if(name == "")
  63.       {
  64.          this.m_isPaused = isPaused;
  65.       }
  66.       else
  67.       {
  68.          this.m_arrIsPaused[name] = isPaused;
  69.       }
  70.    }
  71.    function SetNamedTimeScalar(name, timeScalar)
  72.    {
  73.       if(name == "")
  74.       {
  75.          this.m_timeScalar = timeScalar;
  76.       }
  77.       else
  78.       {
  79.          this.m_arrTimeScalars[name] = timeScalar;
  80.       }
  81.    }
  82.    function GetNamedClock(name)
  83.    {
  84.       if(name == "")
  85.       {
  86.          return this.m_timeSecondsSimulated;
  87.       }
  88.       return this.m_arrClocks[name];
  89.    }
  90.    function GetNamedDeltaTime(name)
  91.    {
  92.       if(name == "")
  93.       {
  94.          return this.m_frameDeltaTimeSimulated;
  95.       }
  96.       return this.m_arrDeltaTimes[name];
  97.    }
  98.    function GetNamedTimeScalar(name)
  99.    {
  100.       if(name == "")
  101.       {
  102.          return this.m_timeScalar;
  103.       }
  104.       return this.m_arrTimeScalars[name];
  105.    }
  106.    function GetNamedIsPaused(name)
  107.    {
  108.       if(name == "")
  109.       {
  110.          return this.m_isPaused;
  111.       }
  112.       return this.m_arrIsPaused[name];
  113.    }
  114.    function UpdateNamedClock(name)
  115.    {
  116.       FreshDebug.Assert(this.HasNamedClock(name),"HasNamedClock( name )");
  117.       if(!this.m_arrIsPaused[name])
  118.       {
  119.          if(this.m_constantFrameDeltaTime)
  120.          {
  121.             this.m_arrDeltaTimes[name] = this.m_constantFrameDeltaTime * this.m_arrTimeScalars[name];
  122.          }
  123.          else
  124.          {
  125.             this.m_arrDeltaTimes[name] = this.m_frameDeltaTimeReal * this.m_arrTimeScalars[name];
  126.          }
  127.          this.m_arrDeltaTimes[name] = Math.min(this.m_arrDeltaTimes[name],this.m_maxFrameDeltaTime);
  128.          this.m_arrClocks[name] += this.m_arrDeltaTimes[name];
  129.       }
  130.       else
  131.       {
  132.          this.m_arrDeltaTimes[name] = 0;
  133.       }
  134.    }
  135.    function UpdateAllClocks()
  136.    {
  137.       var _loc3_ = this.m_timeSecondsReal;
  138.       this.m_timeSecondsReal = new Date().getTime() / 1000;
  139.       this.m_frameDeltaTimeReal = this.m_timeSecondsReal - _loc3_;
  140.       if(!this.m_isPaused)
  141.       {
  142.          if(this.m_constantFrameDeltaTime)
  143.          {
  144.             this.m_frameDeltaTimeSimulated = this.m_constantFrameDeltaTime * this.m_timeScalar;
  145.          }
  146.          else
  147.          {
  148.             this.m_frameDeltaTimeSimulated = this.m_frameDeltaTimeReal * this.m_timeScalar;
  149.          }
  150.          this.m_frameDeltaTimeSimulated = Math.min(this.m_frameDeltaTimeSimulated,this.m_maxFrameDeltaTime);
  151.          this.m_timeSecondsSimulated += this.m_frameDeltaTimeSimulated;
  152.       }
  153.       else
  154.       {
  155.          this.m_frameDeltaTimeSimulated = 0;
  156.       }
  157.       for(var _loc2_ in this.m_arrClocks)
  158.       {
  159.          this.UpdateNamedClock(_loc2_);
  160.       }
  161.    }
  162.    function Update()
  163.    {
  164.       this.m_nUpdates = this.m_nUpdates + 1;
  165.       this.UpdateAllClocks();
  166.       if(this.m_shouldSortThings)
  167.       {
  168.          this.m_simulatedThings.sortOn("_updatePriority");
  169.       }
  170.       var _loc4_ = 0;
  171.       while(_loc4_ < this.m_simulatedThings.length)
  172.       {
  173.          this.m_simulatedThings[_loc4_].PreUpdate();
  174.          _loc4_ = _loc4_ + 1;
  175.       }
  176.       _loc4_ = 0;
  177.       while(_loc4_ < this.m_simulatedThings.length)
  178.       {
  179.          this.m_simulatedThings[_loc4_].Update();
  180.          _loc4_ = _loc4_ + 1;
  181.       }
  182.       var _loc2_ = true;
  183.       var _loc5_ = 0;
  184.       while(_loc2_ && _loc5_ < 4)
  185.       {
  186.          _loc2_ = false;
  187.          _loc4_ = 0;
  188.          while(_loc4_ < this.m_simulatedThings.length)
  189.          {
  190.             var _loc3_ = this.m_simulatedThings[_loc4_].UpdateConstraints();
  191.             _loc2_ = _loc2_ || _loc3_;
  192.             _loc4_ = _loc4_ + 1;
  193.          }
  194.          _loc5_ = _loc5_ + 1;
  195.       }
  196.       _loc4_ = 0;
  197.       while(_loc4_ < this.m_simulatedThings.length)
  198.       {
  199.          this.m_simulatedThings[_loc4_].PostUpdate();
  200.          _loc4_ = _loc4_ + 1;
  201.       }
  202.       if(this.m_collisionResolver && this.m_bCheckCollisions)
  203.       {
  204.          this.m_collisionResolver.ResolveCollisions(this.m_bodies);
  205.       }
  206.    }
  207.    function FindBody(body)
  208.    {
  209.       var _loc2_ = 0;
  210.       while(_loc2_ < this.m_bodies.length)
  211.       {
  212.          if(this.m_bodies[_loc2_] == body)
  213.          {
  214.             return _loc2_;
  215.          }
  216.          _loc2_ = _loc2_ + 1;
  217.       }
  218.       return -1;
  219.    }
  220.    function RegisterBody(body)
  221.    {
  222.       var _loc2_ = this.FindBody(body);
  223.       if(_loc2_ < 0)
  224.       {
  225.          this.m_bodies.push(body);
  226.          this.RegisterSimulated(body);
  227.       }
  228.    }
  229.    function UnregisterBody(body)
  230.    {
  231.       var _loc2_ = this.FindBody(body);
  232.       if(_loc2_ >= 0)
  233.       {
  234.          this.m_bodies.splice(_loc2_,1);
  235.       }
  236.       this.UnregisterSimulated(body);
  237.    }
  238.    function FindSimulated(simulated)
  239.    {
  240.       var _loc2_ = 0;
  241.       while(_loc2_ < this.m_simulatedThings.length)
  242.       {
  243.          if(this.m_simulatedThings[_loc2_] == simulated)
  244.          {
  245.             return _loc2_;
  246.          }
  247.          _loc2_ = _loc2_ + 1;
  248.       }
  249.       return -1;
  250.    }
  251.    function RegisterSimulated(simulated)
  252.    {
  253.       var _loc2_ = this.FindSimulated(simulated);
  254.       if(_loc2_ < 0)
  255.       {
  256.          this.m_simulatedThings.push(simulated);
  257.       }
  258.    }
  259.    function UnregisterSimulated(simulated)
  260.    {
  261.       var _loc2_ = this.FindSimulated(simulated);
  262.       if(_loc2_ >= 0)
  263.       {
  264.          this.m_simulatedThings.splice(_loc2_,1);
  265.       }
  266.    }
  267. }
  268.